package com.lqian.controller;

import com.lqian.pojo.Catalog;
import com.lqian.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Optional;

/**
 * @author：Melody_LQ
 * @time：2022/8/19 3:44 下午
 */
@Controller
public class CatalogController {
    @Autowired
    private CatalogService catalogService;


    @PostMapping("/optLockTryLock")
    @ResponseBody
    public String updateCatalogWithVersion(@RequestParam HashMap<String,String> params) throws Exception {
        String catalogId = params.getOrDefault("catalogId","1");
        String user = params.get("user");
        Optional<Catalog> catalogs = catalogService.findCatalogsForUpdate(Long.valueOf(catalogId));
        catalogs.ifPresent(catalog -> {
            catalogService.updateCatalogWithVersion(catalog.getId(), catalog.getBrowseCount()+1, catalog.getVersion());
        });
        return "操作结束";
    }
}
